import java.util.Deque;
import java.util.LinkedList;

public class Test23 {
    public static void printFromToBottom(TreeNode root){
        Deque<TreeNode> deque = new LinkedList<>();
        deque.add(root);
        TreeNode temp = root;
        while (!deque.isEmpty()){
            int size = deque.size();
            while(size-- > 0){
                temp = deque.pop();
                System.out.print(temp.val + " ");
                if (temp.left != null) deque.add(temp.left);
                if (temp.right != null) deque.add(temp.right);
            }
        }
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode();
        root.val = 1;
        root.left = new TreeNode();
        root.left.val = 2;
        root.right = new TreeNode();
        root.right.val = 3;
        root.left.left = new TreeNode();
        root.left.left.val = 4;
        root.left.right = new TreeNode();
        root.left.right.val = 5;
        root.right.left = new TreeNode();
        root.right.left.val = 6;
        root.right.right = new TreeNode();
        root.right.right.val = 7;
        printFromToBottom(root);
    }
}
